﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Command class for "touching" a file, i.e., creating a file.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;
using FileSystem.Utility;

namespace FileSystem.Impl.Commands
{
	class MakeDir : ICommand, ICommandPrototype
	{
		private static MakeDir zombie;
	
		private static void loadEmulator() {
			zombie = new MakeDir(new HelpDescription() { Name = "mkdir", Description = "Creates a directory." });
			ICommandPrototypeRegistry r = AppResources.getInstance().getCommandRegistry();
			r.register("mkdir", zombie);
		}
        
		private IFSDirectory cwd;
		private String dirName;
		private IFSDirectory addedDirectory;
		private bool executeSucceeded;

        private MakeDir(IHelpDescription helpDescription)
        {
			//for zombie
            this.Description = helpDescription;
		}

		private MakeDir(String dirName)
		{
			this.dirName = dirName;
			executeSucceeded = false;
		}

        public IHelpDescription Description
        {
            get;
            private set;
        }
		public ICommand clone() {
			return null;
		}
	
		public ICommand clone(String arg) {
			MakeDir rc = new MakeDir(arg);
			return rc;
		}

		public IResult execute() {
            this.cwd = AppResources.getInstance().getFileSystem().CurrentDirectory;
			INodeMetaDataVO metaData = AppResources.getInstance().getFileSystem().getMetaDataForEmptyNode(dirName);
			FSDirectory file = new FSDirectory(metaData);
			bool rc = cwd.addNode(file);
			if (rc) {
				executeSucceeded = true;
				this.addedDirectory = file;
				return new Result("mkdir successful: " + file.describe());
			}
			executeSucceeded = false;
			return new Result(true, "mkdir failed.");
		}
	
		public void undo() {
			if (!executeSucceeded) {
				//nothing to do, because execution failed...
				//we don't want to remove the node that might have been there of the same name.
				return;
			}
			cwd.removeNode(dirName);
		}
	
		public IResult redo() {
			return execute();
		}
	
		public String describe(IPrintContext context) {
			return context.getFormattedString("mkdir " + dirName + "(" + executeSucceeded + ").");
		}
	}
}
